﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Matrix_network.Code;
using Matrix_network.Code.EntityClasses;

namespace Matrix_network
{
    public partial class Message : System.Web.UI.Page
    {
        int msgId;
        int fromId;
        int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpRequest r = Request;
            if (r.QueryString["msgId"] != null && r.QueryString["fromId"] != null)
            {
                fromId = Int32.Parse(r.QueryString["fromId"]);
                id = Int32.Parse(User.Identity.Name);
                msgId = Int32.Parse(r.QueryString["msgId"]);
                MessagesDataSource mds = new MessagesDataSource();
                FromHyperLink.Text = mds.getNameFromId(fromId);
                FromHyperLink.NavigateUrl = "~/ViewProfile.aspx?id="+fromId;
                   
                MessageInfo msg = mds.getInMessage(msgId,id);

                if (msg!=null)
                {
                    img.ImageUrl = "Photo/"+ msg.avaId;
                    InboxMsg.Text = msg.message;
                    TimeLabel.Text = msg.time.ToString();
                }
                else 
                {
                    Response.Redirect("~/Messages.aspx");
                }
            }
            else if (r.QueryString["msgId"] != null && r.QueryString["toId"] != null)
            {
                fromId = Int32.Parse(r.QueryString["toId"]);
                id = Int32.Parse(User.Identity.Name);
                msgId = Int32.Parse(r.QueryString["msgId"]);
                MessagesDataSource mds = new MessagesDataSource();
                FromHyperLink.Text = mds.getNameFromId(fromId);
                FromHyperLink.NavigateUrl = "~/ViewProfile.aspx?id=" + fromId;
                MessageInfo msg = mds.getOutMessage(msgId, id);
                if (msg!=null)
                {
                    img.ImageUrl = "Photo/" + msg.avaId;
                    InboxMsg.Text = msg.message;
                    TimeLabel.Text = msg.time.ToString();
                }
                else
                {
                    Response.Redirect("~/Messages.aspx");
                }
            } 
            else { Response.Redirect("~/Messages.aspx"); }
        }

        protected void SendButton_Click(object sender, EventArgs e)
        {
            if (!ResponseMsg.Text.Equals(""))
            {
                new MessagesDataSource().sentMessage(id, fromId, ResponseMsg.Text);
                Response.Redirect("~/Messages.aspx");
            }
            else
            {
                //Response.Redirect("~/dfgdfggMessages.aspx");
            }
        }

        protected void InboxMsg_TextChanged(object sender, EventArgs e)
        {

        }

        
    }
}